﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MixScript.DBService;
using MixScript.BLL;

namespace MixScript.BLL.WM
{
    public class cDN : cBaseBLL
    {
        string sout = "";
        public cDN()
        {
        }


        public string getDElno()
        {
            return new cDBHelper().genId("D", "DELNO");
        }

        public string getTipeDN(string supcode)
        {
            try
            {
                string s = Dbserver.ExecuteScalar("select top 1 cntyns from vw_joid_customer_v where codfbpid='" + supcode + "'").ToString();

                return s;

            }
            catch (Exception ex)
            {

                throw;
            }
        }

        public string getTipeDNbyDelno(string delno)
        {
            try
            {
                string s = Dbserver.ExecuteScalar("select dntyp from tb_dnmst where delno='" + delno + "'").ToString();

                return s;

            }
            catch (Exception ex)
            {

                throw;
            }
        }

        public bool saveDelNo(string delno, string snp, string dntyp, string showpo, DataTable dt, out string ssout,string scomp)
        {
            try
            {

                ssout = "";
                if (isRecordExist("tb_dnmst", new string[] { "delno" }, new object[] { delno }))
                {

                    ssout = "Delivery Number Already Exist";
                    return false;
                }
                else
                {
                    DataTable dt2 = new DataTable();
                    DataTable dtdtl = new DataTable();
                    DataTable dtsliphdr = new DataTable();
                    DataTable dtslipdtl = new DataTable();
                    getTableStructure(ref dt2, "tb_dnmst");
                    getTableStructure(ref dtdtl, "tb_dndtl");
                    getTableStructure(ref dtsliphdr, "accsliphdr");
                    getTableStructure(ref dtslipdtl, "accslipdet");
                    decimal dc = 0;
                    decimal totdec = 0;
                    foreach (DataRow drr in dt.Rows)
                    {
                        decimal.TryParse(drr["pkqt"].ToString(), out dc);
                        totdec += dc;
                        DataRow drdtl = dtdtl.NewRow();
                        DataRow drslipdet = dtslipdtl.NewRow();
                        drdtl["delno"] = delno;
                        //drdtl["cuid"] = drr["cuid"];
                        drdtl["dvdt"] = drr["dvdt"];
                        drdtl["oaitno"] = drr["oaitno"];
                        drdtl["qty"] = drr["pkqt"];
                        drdtl["uom"] = drr["unmsns"].ToString().Trim();

                        if (drr["snp"].ToString().Trim() == string.Empty)
                        {
                            drdtl["snp"] = drr["snp"];
                        }
                        else
                        {
                            drdtl["snp"] = 0;
                        }



                        drdtl["cuod"] = drr["cuod"];
                        drdtl["cuslno"] = drr["cuslno"];
                        drdtl["pkno"] = drr["pkno"];

                        dtdtl.Rows.Add(drdtl);



                        //string sacc = Dbserver.ExecuteScalar("select fk_modulid from tb_mappingmodulmaterial where fk_materialid='" + drr["oaitno"] + "'").ToString();
                        //string sprice = Dbserver.ExecuteScalar("select price from tb_unitprice where itno='" + drr["oaitno"] + "' and dtprice <= getdate()").ToString();
                        //decimal dcprice = 0;
                        //if(sprice!=string.Empty)                        
                        //dcprice=  decimal.Parse(sprice);



                        
                        //DataTable dtaccc=Dbserver.ExecuteQuery("select top 1 * from VW_MATERIAL_MAPPING where matno='"+ drr["oaitno"]+"'","mapping");
                        //DataRow dracc= dtaccc.Rows[0];



                    //    string sacc = dracc["ACCTNUM"].ToString();// Dbserver.ExecuteScalar("select ACCTNUM from VW_MATERIAL_MAPPING where fk_materialid='" + drr["oaitno"] + "'").ToString();
                        //string sprice = Dbserver.ExecuteScalar("select top 1 tdup from vw_joid_sales_unit_price_v where itno='" + drr["oaitno"].ToString().Replace("."+scomp,"") + "' and aptodt>= getdate()").ToString();
                        //decimal dcprice = 0;
                        //if (sprice != string.Empty)
                        //    dcprice = decimal.Parse(sprice);

                        //drslipdet["id"] = new cDBHelper().genIDCommit("SLIPDTLID");
                        //drslipdet["acctnum"] = sacc;
                        //drslipdet["desc"] = drr["itnm"];
                        //drslipdet["dbcd"] = dracc["dbcd"].ToString();
                        //drslipdet["currency"] = "IDR";
                        //drslipdet["kurs"] = "pajak";
                        //drslipdet["rate"] = "1";
                        //drslipdet["amount"] = dcprice * decimal.Parse(drr["pkqt"].ToString());
                        //drslipdet["valconvert"] = drslipdet["amount"];// "1";
                        //drslipdet["so_id"] = "1";
                        ////drslipdet["fg_id"] = "1";
                        //drslipdet["invoice"] = "";
                        //drslipdet["process"] = dracc["process"].ToString();
                        //drslipdet["subsidiary"] = dracc["CODFBPID"].ToString();

                        //dtslipdtl.Rows.Add(drslipdet);

                    }



                    DataRow dr = dt2.NewRow();
                    dr["delno"] = delno;
                    dr["cuid"] = dt.Rows[0]["cuid"];
                    dr["totqty"] = totdec;
                    dr["snp"] = snp;
                    dr["dntyp"] = dntyp;
                    dr["showpo"] = showpo;

                    dr["totuom"] = dt.Rows[0]["unmsns"].ToString().Trim();
                    dr["dtcreated"] = DateTime.Now.ToLongDateString();

                    new cDBHelper().genIDCommit("D", "DELNO");
                    dt2.Rows.Add(dr);

                    //==========posting acc=============================
                    //int nret = 0;
                    //dr = dtsliphdr.NewRow();
                    //dr["kd_cmpy"] = "01";
                    //dr["slipdate"] = getServerTime();
                    //dr["sliptype"] = "4";
                    //dr["slipCode"] = "MM05";


                    //dr["slipcurr"] = "IDR";
                    //dr["slipdesc"] = "Delivery Notes";
                    //dr["posting"] = 1;
                    //dr["forex"] = 0;
                    //dr["transfer"] = 0;
                    //dr["slipNum"] = Int32.Parse(new cDBHelper().genId("SLIPID")).ToString().PadLeft(7, '0');
                    //dr["slipid"] = new cDBHelper().genIDCommit("SLIPID");
                    //dr["matref"] = delno;


                    //dtsliphdr.Rows.Add(dr);

                    if (dtdtl.Rows.Count > 0)
                    {
                        int nret = 0;
                        //if (!posting2Accounting(dtsliphdr, dtslipdtl, delno, out sout))
                        //    return false;

                        if (Dbserver.openConn())
                        {
                            nret = Dbserver.SqlUpdate(dt2, "tb_dnmst", out ssout);
                            if (nret > 0)
                            {
                                nret = Dbserver.SqlUpdate(dtdtl, "tb_dndtl", out ssout);
                                Dbserver.CommitAndCloseConn();
                            }
                            else
                            {
                                Dbserver.ForceRollBackNClose();
                                return false;
                            }
                        }
                        else
                        {
                            sout = "Could not Open Database";
                            return false;

                        }
                        //Dbserver.SqlUpdate();

                        return nret > 0;
                    }
                    else
                    {
                        sout = "Detail is empty";
                        return false;
                    }
                    //if (Dbserver.openConn())
                    //{
                    //    Dbserver.SqlUpdate(dtdtl, "tb_dndtl", out ssout);
                    //    int nret = Dbserver.SqlUpdate(dt2, "tb_dnmst", out ssout);

                    //    return nret == 1;



                    //}
                    //else
                    //{
                    //    throw new Exception("Could't open Connection");
                    //}




                }
            }
            catch (Exception ex)
            {

                throw;
            }
            finally
            {
                if (Dbserver.StateConn == ConnectionState.Open)
                {
                    Dbserver.CommitAndCloseConn();
                }
            }

        }

        public string getSNP(string delno)
        {
            try
            {
                string ssql = "select snp from tb_dnmst where delno='" + delno + "'";
                return Dbserver.ExecuteScalar(ssql).ToString();


            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        //public string getDNType(string delno)
        //{
        //    return Dbserver.ExecuteScalar("select dntyp from tb_dnmst where delno='" + delno + "'").ToString();
        //}

        public DataTable getDNWithType(string dntyp, string delno)
        {
            try
            {

                DataTable dt = new DataTable();
                string ssql = "select * from vw_mapdngv where delno='" + delno+ "' and dntype='"+dntyp+"'";
                dt = Dbserver.ExecuteQuery(ssql, "");
                return dt;

            }
            catch (Exception ex)
            {

                throw;
            }


        }
        public DataTable getDeliveryNotePrint(string delNo)
        {
            try
            {
                
                DataTable dt = new DataTable();
                string ssql = "select * from vw_mapdngv where delno='" + delNo + "'";
                dt = Dbserver.ExecuteQuery(ssql, "");
                return dt;

            }
            catch (Exception ex)
            {

                throw;
            }


        }
        public DataTable getDeliveryNotePrint(string custid, string sdatetime)
        {
            try
            {
                DataTable dt = new DataTable();
                string ssql = "select * from vw_mapdn where cuid='" + custid + "' and dvdt='" + sdatetime.Substring(6, 4) + "-" + sdatetime.Substring(3, 2) + "-" + sdatetime.Substring(0, 2) + "'";
                dt = Dbserver.ExecuteQuery(ssql, "");
                return dt;

            }
            catch (Exception ex)
            {

                throw;
            }


        }
    }
}
